package wnxy.wn44.shopwn.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import wnxy.wn44.shopwn.entity.User;
import wnxy.wn44.shopwn.service.UserService;
import wnxy.wn44.shopwn.utils.Result;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.List;

@Api("用户控制器")
@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {
    private UserService userService;

    private DataSource dataSource;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @RequestMapping("/login")
    @ApiOperation("用户登录行为")
    public void userlogin(User user, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(user);
        List<User> list = userService.findByCondition(user);
        User loginUser = list.get(0);
        System.out.println(loginUser);
        if (loginUser != null){
            Cookie cookie = new Cookie("userId",String.valueOf(loginUser.getId()));
            cookie.setPath("/");
            response.addCookie(cookie);
            request.getRequestDispatcher("/index.html").forward(request,response);
        }else{
            response.sendRedirect("/login.html");
        }
    }

    @ApiOperation("保存用户")
    @ResponseBody
    @PostMapping("/saveUser")
    public Result saveUser(@ApiParam("要被保存的用户对象") @RequestBody User user){
        log.debug(user.toString());
        int i = userService.saveUser(user);
        if (i != 0){
            return Result.ok().data("saveUser",user);
        }else {
            return Result.error();
        }
    }

    @ApiOperation("根据主键删除用户")
    @ResponseBody
    @GetMapping("/deleteUser/{id}")
    public Result deleteById(@ApiParam("要被删除的用户主键") @PathVariable("id") Integer userid){
       return userService.deleteById(userid)?Result.ok():Result.error();
    }


    @ApiOperation("根据主键查询用户")
    @ResponseBody
    @PostMapping("/findUser/{id}")
    public Result findById(@ApiParam("要查询用户的主键") @PathVariable("id") Integer id){
        User user = userService.findById(id);
        if (user != null){
            return Result.ok().data("User",user);
        }else {
            return Result.error();
        }
    }

    @ApiOperation("分页显示用户信息")
    @ResponseBody
    @PostMapping("/findPageUser")
    public Result fingByPage(@ApiParam("要查找第几页的数据") @RequestParam(value = "pageNum",required = true,defaultValue = "1") Integer pageNum,
                             @ApiParam("每页显示的条数") @RequestParam(value = "pageSize",required = true,defaultValue = "5") Integer pageSize
                             ){
        PageInfo<User> list = userService.findByPage(pageNum,pageSize);
        return Result.ok().data("pageInfo",list);
    }

    @ApiOperation("修改用户")
    @ResponseBody
    @PostMapping("/updateUser")
    public Result updateUser(@ApiParam("要被修改的用户对象") @RequestBody User user){
        int i = userService.updateUser(user);
        if (i != 0){
            return Result.ok().data("updateUser",user);
        }else {
            return Result.error();
        }
    }
}
